<?php
	if( $_SESSION['islogin']==NULL){ header('Location: index.php');}
	
	//Continue code from page cart_content.php
			

			$cart = $_SESSION['cart'];
			$locationprice = $_SESSION['locationPrice'] ;
			if($locationprice == 1){
				if($_SESSION['isAhli'] == 'yes' ){ 
					$priceLocation = 'price_em_ahli';
				}else{ 
					$priceLocation = 'price_em';
				}
			}else{
				if($_SESSION['isAhli'] == 'yes' ){ 
					$priceLocation = 'price_wm_ahli';
				}else{ 
					$priceLocation = 'price_wm';
				}
			}
			if ($cart) {
				$isSemakCheckout = true;
				?>
				<script>
					$('#cartForm').hide();
				</script>
				<?php
				//Dapatkan Info Items
				$items = explode(',',$cart);
				$totalItem = count($items);  //Dapatkan total  qty items
				$contentsItems = array();    //Masukkan maklumat Item and Quantity dalam Array
				foreach ($items as $item) {
					$contentsItems[$item] = (isset($contentsItems[$item])) ? $contentsItems[$item] + 1 : 1;
				}
				
				$amount = 0;
				$items_price = 0;
				$postage_price = 0;
				$total_weight = 0;
				
				echo '<table id="viewCheckOut" width="100%"  cellspacing="0" cellpadding="0" style="border-collapse: collapse; border: 1px solid #CCCCCC;">
						<tr bgcolor="#F1F3EB" height="25">
									<td width="70px" align="center" class="arial11P" style="border: 1px solid #CCCCCC;">'.$lang['FORM_TRANS_BIL'].'
									</td>
									<td  align="center" class="arial11P" style="border: 1px solid #CCCCCC;">'.$lang['FORM_TRANS_PRODUCT'].'
									</td>
									<td width="70px" align="center" class="arial11P" style="border: 1px solid #CCCCCC;">'.$lang['FORM_TRANS_QTY'].'
									</td>
									<td width="80px" align="center" class="arial11P" style="border: 1px solid #CCCCCC;">'.$lang['FORM_TRANS_TOTAL_ITEMS'].'
									</td>
									<td width="80px" align="center" class="arial11P" style="border: 1px solid #CCCCCC;">'.$lang['FORM_TRANS_TOTAL_WEIGHT'].'
									</td>
						</tr>';
				$bil = 0;
				//Dapatkan Setiap Item Berapa Quantity  :- Mulakan pengiraan
				foreach ($contentsItems as $id=>$qty) {
					$bil++;
					$sql = 'SELECT * FROM item WHERE id = '.$id;
					$rCheck= $db->sql_total($sql);
					$rItem= $db->sql_list($sql);
					if($rCheck <> 0){
						echo '	
							<tr>
								<td width="70px" align="center" style="border: 1px solid #CCCCCC;">
									'.$bil.'
								</td>
								<td width="300px" align="left" style="border: 1px solid #CCCCCC;">
									<table width="100%" border="0" cellspacing="0" cellpadding="0">
										<tr> 
											<td align="left" class="arial11P"> 
												<div style="margin:4px; border:1px solid #CCCCCC;"> 
													<img src="'.IMAGEITEM.$rItem['image'].'"  width="87" height="68"  >
												</div>
											</td>
											<td width="10">&nbsp;</td>
											<td valign="top" width="100%">
												<table width="100%" border="0" cellspacing="0" cellpadding="0">
													<tr> 
														<td height=2></td>
													</tr>
													<tr> 
														<td class="arial11P"><strong><a href="product_detail.php?id='.$rItem[id].'" >'.$rItem['name'].'</a></strong></td>
													</tr>
													<tr> 
														<td height="3" valign="top"><img src="img/spec_blank.gif" width="10" height="3"></td>
													</tr>
													<tr> 
														<td class="arial10G660">KOD 
															'.$rItem['kod'].'</td>
													</tr>
													<tr> 
														<td height="3" valign="top"><img src="img/spec_blank.gif" width="10" height="3"></td>
													</tr>
													<tr> 
														<td class="arial11P"><strong>'.$lang['FORM_PRICE'].'   
															RM'.sprintf("%01.2f", $rItem[$priceLocation]).'/Unit</strong>
														</td>
													</tr>
													<tr> 
														<td height="3" valign="top"><img src="img/spec_blank.gif" width="10" height="3"></td>
													</tr>
													<tr> 
														<td class="arial11P"><strong>'.$lang['FORM_WEIGHT'].'
															'.$rItem['weight'].'KG/Unit</strong>
														</td>
													</tr>
												</table>
											</td>
										</tr>
									</table>
								</td>
								<td  align="center" style="border: 1px solid #CCCCCC;">
									'.$qty.'
								</td>
								<td  align="center" style="border: 1px solid #CCCCCC;" class="arial11P">RM'.sprintf("%01.2f", ($rItem[$priceLocation] * $qty)).'
								</td>
								<td " align="center" style="border: 1px solid #CCCCCC;" class="arial11P">'.($rItem['weight'] * $qty).'KG
								</td>
							</tr>';
						$items_price += $rItem[$priceLocation] * $qty;
						$total_weight += $rItem['weight'] * $qty;
					}
				}
							
				//start Insert Address Postage
				$addressID = 0;
				if($chooseAddress == 'billaddress'){
					$addressID = $mailing_address_id;
				}
				if($chooseAddress == 'shipaddress'){
					$addressID = $shipping_address_id;
				}
				
				$addressToPostID = 0;
				if($addressID > 0) {
					$qAddr = "SELECT * from address where id = '$addressID'";
					$cAddr= $db->sql_total($qAddr);
					$rAddr= $db->sql_list($qAddr);
					
					$cartCfm ['addrNama'] = $rAddr['nama'];
					$cartCfm ['addrNoTel'] = $rAddr['notel'];
					$cartCfm ['addrAddress'] = $rAddr['address'];
					$cartCfm ['addrPoskod'] = $rAddr['poskod'];
					$cartCfm ['addrCity'] = $rAddr['city'];
					$cartCfm ['addrState'] = $rAddr['state_id'];
					$cartCfm ['addrCountry'] = $rAddr['country_id'];

					//$shipper_id  : Get from cart_content.php
					$qLoc = "SELECT w.*
									FROM shipper p ,location i,states s,location_trace_state t,weight w
									WHERE p.id = i.shipper_id 
										AND i.id = t.location_id 
										AND s.id = t.state_id 
										AND w.location_id = i.id
										AND p.id = '$shipper_id'
										AND s.id = '$rAddr[state_id]' 
										AND w.min_weight < CEILING($total_weight) 
										AND w.max_weight >= FLOOR($total_weight) limit 0,1 ";
					$cLoc= $db->sql_total($qLoc);
					$rLoc= $db->sql_list($qLoc);
					if($cLoc > 0){
						$postage_price = $rLoc['price'];
					}
					
					//Total keseluruhan : itemPrice + harga postage
					$amount  = $items_price + $postage_price;
				}
				echo '		<tr bgcolor="#FFF380" height="25">
								<td colspan="2"  align="right" style="border: 1px solid #CCCCCC;" class="arial11Pbold">'.$lang['FORM_TRANS_TOTAL_ALLITEMS'].' &nbsp;&nbsp;
								</td>
								<td  colspan="3" align="left" style="border: 1px solid #CCCCCC;" class="arial11Pbold"> &nbsp;&nbsp; RM'.sprintf("%01.2f", $items_price).'
								
								</td>
							</tr>';
				echo '		<tr bgcolor="#FFF380" height="25">
								<td colspan="2"  align="right" style="border: 1px solid #CCCCCC;" class="arial11Pbold">'.$lang['FORM_TRANS_TOTAL_ALLWEIGHT'].' &nbsp;&nbsp;
								</td>
								<td  colspan="3" align="left" style="border: 1px solid #CCCCCC;" class="arial11Pbold"> &nbsp;&nbsp; RM'.sprintf("%01.2f", $postage_price).' for '.$total_weight.'KG
								
								</td>
							</tr>';
				echo '			<tr bgcolor="#FFF380" height="25">
								<td colspan="2"  align="right" style="border: 1px solid #CCCCCC;" class="arial11Pbold">'.$lang['FORM_TRANS_TOTAL_ALL'].' &nbsp;&nbsp;
								</td>
								<td  colspan="3"  align="left"  class="arial11Pbold" style="border: 1px solid #CCCCCC; font-size:14;"> &nbsp;&nbsp; RM'.sprintf("%01.2f", $amount).'
								
								</td>
							</tr>';
				echo '</table>';
				
				
				if($amount > 0){
					$type_price = "Harga Biasa";
					if($_SESSION['isAhli'] == 'yes' ){ 
						$type_price = "Harga Untuk Keahlian Ana Edar";
					}
					
					$cartCfm ['checkTypePrice'] = $type_price;
					$cartCfm ['checkTotalQtyItems'] = $totalItem;
					$cartCfm ['checkAmount'] = $amount;
					$cartCfm ['checkItemsPrice'] =$items_price;
					$cartCfm ['checkPostagePrice'] =$postage_price;
					$cartCfm ['checkTotalWeight'] =$total_weight;
					$cartCfm ['checkContentsItems'] = $contentsItems;
					$cartCfm ['checkPriceLocation'] = $priceLocation;
					$_SESSION['cartConfirmCheckout'] = $cartCfm;
					//print_r($_SESSION['cartConfirmCheckout']);
				}else{
					echo '
							<table width=500 align=center border=0 cellspacing=0 cellpadding=0>
							<tr>
								<td>
							<div style="margin-bottom:10px;font-family: tahoma; font-size: 11px;">';
							echo '<p style="padding:0px; margin:0px; color:red;"><b>'.$lang[CONTENT_ERROR_OCCUR].'</b></p>';
							
							echo '<span style="padding:10px;color:red;">Error in processing!</span><br />';
							
							echo '</div> </td></tr></table>';
				}
			}

?>